package com.jss.lanouhn.fristapp.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.Toast;

import com.jss.lanouhn.fristapp.R;
import com.jss.lanouhn.fristapp.activity.CartActivity;
import com.jss.lanouhn.fristapp.activity.LoginActivity;
import com.jss.lanouhn.fristapp.activity.MipcaActivityCapture;
import com.jss.lanouhn.fristapp.activity.ProfileMessageActivity;
import com.jss.lanouhn.fristapp.activity.SettingActivity;
import com.jss.lanouhn.fristapp.adapters.ProfileVPAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lanouhn on 16/8/23.
 */
public class Fragment_Profile extends Fragment {

    private final static int SCANNIN_GREQUEST_CODE = 1;

    private List<String> tabNames;
    private List<Fragment> fragments;
    private TabLayout tablayout;
    private ViewPager vp;
    private ProfileVPAdapter vpAdapter;
    private ImageView iv_scanf, iv_setting, iv_message, iv_profile;
    private RadioButton rb_follow,rb_cart,rb_order,rb_coupon,rb_service;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_profile, null);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
        initData();
        initEvent();

    }


    private void initView() {
        tablayout = (TabLayout) getView().findViewById(R.id.tl_profile);
        vp = (ViewPager) getView().findViewById(R.id.vp_profile);
        iv_scanf = (ImageView) getView().findViewById(R.id.iv_scanf);
        iv_setting = (ImageView) getView().findViewById(R.id.iv_setting);
        iv_message = (ImageView) getView().findViewById(R.id.iv_profile_message);
        iv_profile = (ImageView) getView().findViewById(R.id.iv_profile);
        rb_follow = (RadioButton) getView().findViewById(R.id.rb_follow);
        rb_cart = (RadioButton) getView().findViewById(R.id.rb_cart);
        rb_order = (RadioButton) getView().findViewById(R.id.rb_order);
        rb_coupon = (RadioButton) getView().findViewById(R.id.rb_coupon);
        rb_service = (RadioButton) getView().findViewById(R.id.rb_service);



    }

    private void initData() {
        tabNames = new ArrayList<>();
        tabNames.add("单品");
        tabNames.add("攻略");

        fragments = new ArrayList<>();
        fragments.add(new Fragment_Profile_Sample());
        fragments.add(new Fragment_Profile_Strategy());
        vpAdapter = new ProfileVPAdapter(getChildFragmentManager(), tabNames, fragments);
//        将适配器和ViewPager绑定
        vp.setAdapter(vpAdapter);
//        将tablayout和View建立关联
        tablayout.setupWithViewPager(vp);

    }



    private void initEvent() {



        iv_scanf.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(getActivity(), MipcaActivityCapture.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivityForResult(intent, SCANNIN_GREQUEST_CODE);
            }
        });

        iv_setting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), SettingActivity.class);
                startActivity(intent);
            }
        });

        iv_message.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), ProfileMessageActivity.class);
                startActivity(intent);
            }
        });
        iv_profile.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(getActivity(), LoginActivity.class);
                startActivity(intent);
            }
        });


        rb_follow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(getActivity(), LoginActivity.class);
                startActivity(intent);
            }
        });

        rb_cart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), CartActivity.class);
                startActivity(intent);
            }
        });



    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case SCANNIN_GREQUEST_CODE:
                if (resultCode == getActivity().RESULT_OK) {
                    Bundle bundle = data.getExtras();

                    Toast.makeText(getActivity(), "扫描的结果是：" + bundle.getString("result"), Toast.LENGTH_SHORT).show();

                }
                break;
        }
    }




}
